package action.FrontAction;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import entity.CroAddress;
import entity.CroCustomer;
import entity.CroMyqpon;
import entity.SysDict;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import service.CroAddressService;
import service.CroCustomerService;
import service.CroMyqponService;
import service.SysDictService;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.xml.ws.spi.http.HttpContext;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xuweijie on 2017/4/5.
 */
public class LoginAction extends ActionSupport {

    @Autowired
    private CroCustomerService croCustomerService;
    @Autowired
    private CroMyqponService croMyqponService;
    private String username;
    private String password;
    private String type;
    private String successResult;
    private CroCustomer croCustomer;

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }

    /*
    * 用户登录
    * */
    public String cuslogin() {
        CroCustomer croCustomer= croCustomerService.FindCusByEmailOrPhone(username,type);
        if(croCustomer!=null){
            if((new Md5Hash(password,croCustomer.getSalt())).toString().equals(croCustomer.getPasswd())){
                ServletContext context= ServletActionContext.getServletContext();
                context.setAttribute("customer",croCustomer);
                List<CroMyqpon> myqponlist=croMyqponService.FindUseMyqponByCusId(croCustomer.getId());
                if(myqponlist.size()>0){//判断当前用户的优惠券是否过期
                    for(CroMyqpon croMyqpon:myqponlist){
                        long overdate=croMyqpon.getOffdate().getTime();
                        long currentdate=new Date().getTime();
                        long time=currentdate-overdate;
                        if(time>0){
                            croMyqpon.setStatus("2");
                            croMyqponService.updateMyqponByEntity(croMyqpon);
                        }
                    }
                }
                successResult="0";
            }else {
                successResult="2";
            }
        }else{
            successResult="1";//登录失败(账号出错)
        }
        return SUCCESS;
    }

    /*
    * 用户退出
    * */
    public String logout(){
        ServletContext context= ServletActionContext.getServletContext();
        context.removeAttribute("customer");
        return SUCCESS;
    }
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSuccessResult() {
        return successResult;
    }

    public void setSuccessResult(String successResult) {
        this.successResult = successResult;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}
